#include "ModelVertexBuffer.h"

ModelVertexBuffer::ModelVertexBuffer(BYTE* vertexData, UINT64 numVertices, UINT64 sizeBytes, UINT64 strideBytes)
{
	pVertexBuffer = NULL;

	this->vertexData = new BYTE[(size_t)sizeBytes];
	memcpy(this->vertexData, vertexData, (size_t)sizeBytes);
	this->numVertices = numVertices;
	this->sizeBytes = sizeBytes;
	this->strideBytes = strideBytes;
}

ModelVertexBuffer::~ModelVertexBuffer(void)
{
	delete this->vertexData;
	UnloadGraphicsContent();
}

HRESULT ModelVertexBuffer::LoadGraphicsContent(EnginePointer<ID3D10Device> pDevice)
{
	D3D10_BUFFER_DESC bd;
	bd.Usage = D3D10_USAGE_DEFAULT;
	bd.ByteWidth = (UINT)sizeBytes;
	bd.BindFlags = D3D10_BIND_VERTEX_BUFFER;
	bd.CPUAccessFlags = 0;
	bd.MiscFlags = 0;

	D3D10_SUBRESOURCE_DATA InitData;
	InitData.pSysMem = vertexData;

	ID3D10Buffer* buffer;
	HRESULT hr = pDevice->CreateBuffer(&bd,& InitData,& buffer);
	if (FAILED(hr)) return hr;

	pVertexBuffer = buffer;
	buffer->Release();

	return S_OK;
}

void ModelVertexBuffer::UnloadGraphicsContent()
{
	pVertexBuffer = 0;
}

EnginePointer<ID3D10Buffer> ModelVertexBuffer::getBuffer()
{
	return pVertexBuffer;
}

UINT64 ModelVertexBuffer::getStride()
{
	return strideBytes;
}